package cn.example.zjgsu.controller;

import cn.example.zjgsu.pojo.result.JsonData;
import cn.example.zjgsu.service.CommentServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author xuejiahui
 * @date 2020/3/29 -14:31
 */
@RestController
@RequestMapping({"/v1/api/comment"})
@Api(description = "评论接口")
public class CommentController {
    @Autowired
    CommentServer commentServer;

    @ApiOperation("新增店铺评论")
    @PostMapping(value = "/insertShopCommentWithUserid")
    public JsonData insertShopComment(@RequestParam("file")MultipartFile[] files, HttpServletRequest request, @RequestParam("content") String content, @RequestParam("shopid")int shopid, @RequestParam("userid")String userid){
        try {
            return JsonData.success(commentServer.insertShopComment(files,request,content,shopid,userid));
        } catch (IOException e) {
            return JsonData.fail("评论图片插入失败,请重新发表评论");
        }
    }

    @ApiOperation("新增店铺评论第二版")
    @PostMapping(value = "/insertShopCommentWithUserid2")
    public JsonData insertShopComment2(@RequestParam("image0") MultipartFile image0,@RequestParam("image1") MultipartFile image1,@RequestParam("image2") MultipartFile image2, HttpServletRequest request, @RequestParam("content") String content, @RequestParam("shopid")int shopid, @RequestParam("userid")String userid){
        try {
            System.out.println(image0.getOriginalFilename());
            System.out.println(image1.getOriginalFilename());
            System.out.println(image2.getOriginalFilename());
            return JsonData.success(commentServer.insertShopComment2(image0,image1,image2,request,content,shopid,userid));
        } catch (IOException e) {
            return JsonData.fail("评论图片插入失败,请重新发表评论");
        }
    }

    @ApiOperation("新增食记评论")
    @PostMapping(value = "/insertFoodRecordCommentWithUserid")
    public JsonData insertFoodRecordComment(String content, int foodrecordid, String userid){
        return JsonData.success(commentServer.insertFoodRecordComment(content,foodrecordid,userid));
    }

    @ApiOperation("获得食记评论")
    @GetMapping(value = "/getFoodRecordCommentByFoodRecordId")
    public JsonData getFoodRecordCommentByFoodRecordId(int foodrecordid){
        return JsonData.success(commentServer.getFoodRecordComment(foodrecordid));
    }

    @ApiOperation("获得店铺评论")
    @GetMapping(value = "/getShopCommentByShopId")
    public JsonData getShopCommentByShopId(int shopid){
        return JsonData.success(commentServer.getShopComment(shopid));
    }

    @ApiOperation("点赞评论")
    @PostMapping(value = "/praiseComment")
    public JsonData praiseComment(int commentid){
        return JsonData.success(commentServer.praiseComment(commentid));
    }

    @ApiOperation("删除评论")
    @DeleteMapping(value = "/deletecomment")
    public JsonData deleteComment(int commentid){
        return JsonData.success(commentServer.deleteComment(commentid));
    }


}
